<?php

/**
 * Class CRedis
 */
class CRedis
{
    public $host = '127.0.0.1';

    public $port = 6379;

    public $database = 0;

    public $auth = 'zhaoguangsheng';

    public $redis;

    /**
     * @return \Redis
     */
    public static function getRedis(){
        static $redis = null;
        if($redis === null) $redis = new \CRedis();
        return $redis->getInstance();
    }


    private function connect()
    {
        $link = new \Redis();
        $link->connect($this->host, $this->port, $this->database);
        $link->auth($this->auth);
        return $link;
    }

    /**
     * @return mixed|\Redis
     */
    private function getInstance()
    {
        if ($this->redis instanceof \Redis && $this->redis->ping()) {
            return $this->redis;
        }
        $this->redis = $this->connect();
        return $this->redis;
    }
}